import {
  setStorageToken,
  getStorageToken,
  removeStorageToken
} from '@/utils/storage'
import { getUserInfo } from '@/api/user'
// import { Toast } from 'vant'

const state = {
  token: getStorageToken(),
  userInfo: {}
}

const mutations = {
  setToken(state, payload) {
    state.token = payload
    setStorageToken(payload)
  },
  setUserInfo(state, payload) {
    state.userInfo = payload
  },
  removeToken(state) {
    state.token = {}
    removeStorageToken()
  }
}

const getters = {}
const actions = {
  async getUserInfo(context) {
    try {
      const res = await getUserInfo()
      context.commit('setUserInfo', res.data)
      // this.setUserInfo(res.data)
    } catch {
      // Toast('获取失败')
      // this.$router.push('/login')
    }
  }
}

export default {
  state,
  mutations,
  getters,
  actions,
  // 开启命名空间
  namespaced: true
}
